//
//  HLStatusesViewModel.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/21.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLStatusesViewModel.h"
#import "HLUserAccountViewModel.h"
#import "HLNetworking.h"
#import "HLStatuses.h"

@implementation HLStatusesViewModel

+ (void)loadHomeStatusesDataWithSinceId:(NSString *)since_id withMaxId:(NSString *)max_id withFinshedBlock:(void (^)(NSArray *statusesArr))finshedBlock {
    //先判断是否有用户的token,如果有,再进行网络请求
    NSString *userToken = [HLUserAccountViewModel getUserToken];
    if (!userToken) {
        return;
    }
    //设置参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"access_token"] = userToken;
    //根据修改参数
    if (since_id != nil) {
        parameters[@"since_id"] = since_id;
    }else if (max_id != nil) {
        parameters[@"max_id"] = [NSString stringWithFormat:@"%lld",[max_id longLongValue] - 1];
    }
    //请求网络数据
    [[HLNetworking sharedNetworing] sendHTTPRequestWithHTTPMethod:@"GET" withUrlStr:@"2/statuses/home_timeline.json" withParameters:parameters withFinshedBlock:^(id resultData, NSError *error) {
        //判断是否成功
        if (error == nil) {
            //获取状态信息
            NSArray *statusesDicArr = resultData[@"statuses"];
            //创建模型数组
            NSMutableArray *arrM = [NSMutableArray array];
            //数组状态信息数组
            for (NSDictionary *statusesDic in statusesDicArr) {
                //字典转模型
                HLStatuses *statuses = [HLStatuses statusesWithDictionary:statusesDic];
                //存入可变数组
                [arrM addObject:statuses];
            }
            finshedBlock(arrM);
        }
    }];

}

@end
